package com.ccx.coachsite.versionupdate;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.text.format.Time;

/**
 * <br>Description: 软件更新服务类
 */
public class SoftUpdateService extends Service {

	/**
	 * 一小时对应的毫秒数
	 */
	public final static int ONE_HOUR_MILLISECOND = 3600000;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		// 下次定时请求
		long updateTime = ONE_HOUR_MILLISECOND * 24;
		Intent updateIntent=new Intent();   
        updateIntent.setClass(this, SoftUpdateService.class);   
        PendingIntent pending=PendingIntent.getService(this, 0, updateIntent, 0); 
        
		Time time = new Time();   
        long nowMillis = System.currentTimeMillis();
        time.set(nowMillis + updateTime);
        long updateTimes = time.toMillis(true);
           
        AlarmManager alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);   
        alarm.set(AlarmManager.RTC_WAKEUP, updateTimes, pending);
		SoftVersionUtils.checkForUpdate(SoftUpdateService.this);
        stopSelf();
	}

}
